/**   
* @Title: ListPreference.java 
* @Package com.jiuyang.baoxian.widget 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ChangJianlong   
* @date 2014-12-17 上午10:50:07 
* @version V1.0   
*/
package com.bxd.baodan.widget;

import android.content.Context;
import android.content.Intent;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.DialogUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.widget.InsureHelperListDialog.ItemClickListener;

/** 
 * @ClassName: ListPreference 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author ChangJianlong 
 * @date 2014-12-17 上午10:50:07 
 *  
 */
public class ListPreference extends Preference {
	private TextView value;
	public ListPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		// TODO Auto-generated method stub
		return LayoutInflater.from(getContext()).inflate(
				R.layout.layout_for_edit_preference, parent, false);
	}

	/* (非 Javadoc) 
	* <p>Title: onBindView</p> 
	* <p>Description: </p> 
	* @param view 
	* @see android.preference.Preference#onBindView(android.view.View) 
	*/
	@Override
	protected void onBindView(View view) {
		value = (TextView) view.findViewById(R.id.value);
		try {
			value.setText(sexes[Integer.parseInt(LoginInfo.getInstance(getContext()).getUser().getUe_sex())-1]);
		} catch (Exception e) {
			value.setText(sexes[Integer.parseInt(LoginInfo.getInstance(getContext()).getUser().getUe_sex())]);
			// TODO: handle exception
		}
		super.onBindView(view);
	}

	 
	
	/* (非 Javadoc) 
	* <p>Title: onClick</p> 
	* <p>Description: </p>  
	* @see android.preference.DialogPreference#onClick() 
	*/
	@Override
	protected void onClick() {
		showDialog();
	}
	private void doSelected(int position){
		if(SharedPreferanceKey.SEX.equals(this.getKey())){
			sendRequest(position + 1);
		}else{
			ListPreference.this.persistString(sexes[position]);
		}
	}
	private void sendRequest(final int sex){
		NetUtil netUtil = new NetUtil((PreferenceActivity)this.getContext(), JsonApi.MODIFYINFOR);
		netUtil.setParams("sex",sex);
		netUtil.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					ListPreference.this.persistString(sexes[sex-1]);
					SpUtil.getInstance().savaString(getKey(), sexes[sex-1]);
					
					UserItem item = LoginInfo.getInstance(
							getContext().getApplicationContext())
							.getUser();
					item.setUe_sex(sex+"");
					LoginInfo.getInstance(getContext())
							.saveUserInfo(new Gson().toJson(item),
									getContext());
					
					
//					LoginInfo.getInstance(getAty()).getUser().setUe_sex(sex+"");
					ListPreference.this.getContext().sendBroadcast(new Intent(Constant.ACTION_SUCCESS_UPLOAD_INFOR));
				} else {
					DialogUtil.getInstance().showToast(ListPreference.this.getContext(),
							JSONUtil.getMessage(result));
				}
			}
		});
	}
	String[] sexes = {"男","女"};
	private void showDialog() {
		InsureHelperListDialog ihld = new InsureHelperListDialog(this.getContext(), null, sexes,0,0);
		ihld.setItemClickListener(new ItemClickListener() {
			
			@Override
			public void onItemClick(int position) {
				doSelected(position);
			}
		});
		ihld.show();		
	}

	 
}
